package com.bs.regsystemapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.regsystemapi.dao.MessageDao;
import com.bs.regsystemapi.entity.Actor;
import com.bs.regsystemapi.entity.Messages;
import com.bs.regsystemapi.modal.dto.file.UpdateFilePathForm;
import com.bs.regsystemapi.modal.dto.message.MessageFrom;
import com.bs.regsystemapi.modal.dto.message.UpdateFileUploadTimeForm;
import com.bs.regsystemapi.modal.dto.message.UpdateReadStatusForm;
import com.bs.regsystemapi.modal.vo.MessageInfo;
import com.bs.regsystemapi.modal.vo.UserInfo;
import com.bs.regsystemapi.modal.vo.patient.PatientInfo;
import com.bs.regsystemapi.service.MessageService;
import com.bs.regsystemapi.service.PatientService;
import com.bs.regsystemapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ning
 * @date 2023/1/12 14:51
 */
@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageDao, Messages> implements MessageService {

    @Autowired
    private UserService userService;
    @Autowired
    private PatientService patientService;

    @Override
    public List<MessageInfo<UserInfo, UserInfo>> getMessage(MessageFrom from) {
        return this.baseMapper.getMessage(from);
    }

    @Override
    public List<MessageInfo<UserInfo, PatientInfo>> getMessageDoctorToPatient(MessageFrom from) {
        return this.baseMapper.getMessageDoctorToPatient(from);
    }

    @Override
    public List<MessageInfo<PatientInfo, UserInfo>> getMessagePatientToDoctor(MessageFrom from) {
        return this.baseMapper.getMessagePatientToDoctor(from);
    }

    @Override
    public List<MessageInfo<UserInfo, UserInfo>> getMessageByNo(String receiverNo) {
        return this.baseMapper.getMessageByNo(receiverNo);
    }

    @Override
    public List<Object> getMessageByActorNo(String actorNo) {
        List<Object> resultObj = new ArrayList<>();
        List<MessageInfo<UserInfo, PatientInfo>> messageByActorNo = this.baseMapper.getMessageByActorNo(actorNo);
        List<MessageInfo<PatientInfo, UserInfo>> messageByReceiverNo = this.baseMapper.getMessageByReceiverNo(actorNo);

        // 遍历当前患者发送的消息
        for (MessageInfo<UserInfo, PatientInfo> actorInfo : messageByActorNo) {
            // 遍历当前患者接受的消息
            for (MessageInfo<PatientInfo, UserInfo> receiverInfo : messageByReceiverNo) {
                // 分组：接受者No等于发送者No，并且发送者No等于接收者No，则是一组
                if (receiverInfo.getActorNo().equals(actorInfo.getReceiverNo()) && receiverInfo.getReceiverNo().equals(actorInfo.getActorNo())) {
                    // 拿到最新消息，作为患者的消息时间小于作为医生的消息时间，则首页展示的为发送者(医生)
                    if (receiverInfo.getSendTime().getTime() > actorInfo.getSendTime().getTime()) {
                        // 添加进结果集
                        UserInfo receiver = userService.getBaseUserInfo(receiverInfo.getActorNo());
                        receiverInfo.setReceiver(new Actor<>(receiver));
                        PatientInfo actor = patientService.getBasePatientInfo(receiverInfo.getReceiverNo());
                        receiverInfo.setActor(new Actor<>(actor));
                        resultObj.add(receiverInfo);

                    } else {
                        // 不是最新消息，则说明发送者的是最新消息
                        UserInfo actor = userService.getBaseUserInfo(receiverInfo.getReceiverNo());
                        actorInfo.setActor(new Actor<>(actor));
                        PatientInfo receiver = patientService.getBasePatientInfo(receiverInfo.getActorNo());
                        actorInfo.setReceiver(new Actor<>(receiver));
                        resultObj.add(actorInfo);
                    }
                } else {
                    UserInfo actor = userService.getBaseUserInfo(actorInfo.getReceiverNo());
                    actorInfo.setActor(new Actor<>(actor));
                    PatientInfo receiver = patientService.getBasePatientInfo(actorInfo.getActorNo());
                    actorInfo.setReceiver(new Actor<>(receiver));
                    resultObj.add(actorInfo);
                }
            }
        }
        return resultObj;
    }

    @Override
    public int getNewsCount(String receiverNo) {
        return this.baseMapper.getNewsCount(receiverNo);
    }

    @Override
    public int updateReadStatus(UpdateReadStatusForm form) {
        return this.baseMapper.updateReadStatus(form);
    }

    @Override
    public int updateFilePathForm(UpdateFilePathForm form) {
        return this.baseMapper.updateFilePathForm(form);
    }

    @Override
    public int updateFileUploadTime(UpdateFileUploadTimeForm form) {
        return this.baseMapper.updateFileUploadTime(form);
    }
}
